package com.ruoyi.common.utils.poi;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.*;

/**
 * Excel默认样式
 * @author  luwl
 */
public class ExcelCellPojo {
    /**
     * 表头样式
     */
    private CellStyle headerStyle;
    /**
     * 默认单元格样式
     */
    private CellStyle cellStyle;
    /**
     * 标题栏样式
     */
    private CellStyle titleCellStyle;
    /**
     * 日期单元格样式
     */
    private  CellStyle dateCellStyle;
    /**
     * 数字类型单元格样式
     */
    private CellStyle doubleCellStyle;

    public ExcelCellPojo(Workbook wb){
        this.setHeaderStyle(wb);
        this.setCellStyle(wb);
        this.setDateCellStyle(wb);
        this.setTitleCellStyle(wb);
        this.setDoubleCellStyle(wb);
    }

    public CellStyle getHeaderStyle() {
        return headerStyle;
    }

    public CellStyle getCellStyle() {
        return cellStyle;
    }

    public CellStyle getDateCellStyle() {
        return dateCellStyle;
    }

    public CellStyle getTitleCellStyle() {
        return titleCellStyle;
    }

    public CellStyle getDoubleCellStyle() {
        return doubleCellStyle;
    }

    private void setHeaderStyle(Workbook wb){
        // 生成表头单元样式
        headerStyle = wb.createCellStyle();
        //表头样式
        headerStyle.setFillForegroundColor((short)1);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //表头边框
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);
        //水平方向-居中对齐
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直方向-垂直居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //字体
        Font font = wb.createFont();
        //字体大小
        font.setFontHeightInPoints((short) 16);
        //字体加粗
        font.setBold(true);
        headerStyle.setFont(font);
    }

    private void setCellStyle(Workbook wb){
        // 生成单元格式样（基础式样）
        cellStyle = wb.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        //水平方向-居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直方向-垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setWrapText(true);
    }

    private void setDateCellStyle(Workbook wb){
        // 生成单元格式样-日期类型（yyyy/mm/dd日期格式）
        dateCellStyle = wb.createCellStyle();
        dateCellStyle.cloneStyleFrom(this.cellStyle);
        DataFormat format = wb.createDataFormat();
        dateCellStyle.setDataFormat(format.getFormat("yyyy/mm/dd"));
    }

    private void setTitleCellStyle(Workbook wb) {
        //生成标题单元格样式
        titleCellStyle = wb.createCellStyle();
        //表头样式
        titleCellStyle.setFillForegroundColor((short)1);
        titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //表头边框
        titleCellStyle.setBorderBottom(BorderStyle.THIN);
        titleCellStyle.setBorderLeft(BorderStyle.THIN);
        titleCellStyle.setBorderRight(BorderStyle.THIN);
        titleCellStyle.setBorderTop(BorderStyle.THIN);
        //水平方向-居中对齐
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直方向-垂直居中
        titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //字体
        Font font = wb.createFont();
        //字体大小
        font.setFontHeightInPoints((short) 20);
        titleCellStyle.setFont(font);
    }

    private void setDoubleCellStyle(Workbook wb) {
        // 生成单元格式样（基础式样）
        doubleCellStyle = wb.createCellStyle();
        doubleCellStyle.setBorderBottom(BorderStyle.THIN);
        doubleCellStyle.setBorderLeft(BorderStyle.THIN);
        doubleCellStyle.setBorderRight(BorderStyle.THIN);
        doubleCellStyle.setBorderTop(BorderStyle.THIN);
        //水平方向-居中对齐
        doubleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直方向-垂直居中
        doubleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        doubleCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
        doubleCellStyle.setWrapText(true);
    }

}